package com.wjy.common.config.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

/**
 * @author ybxxszl
 * @desc SpringSecurity配置
 * @data 2019年4月12日
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private PersistentTokenRepository persistentTokenRepository;

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.formLogin().loginPage("/login").loginProcessingUrl("/login/form").failureUrl("/login_failure")
                .permitAll().and().rememberMe().tokenRepository(persistentTokenRepository).userDetailsService(userDetailsService)
                .tokenValiditySeconds(7 * 24 * 60 * 60).and().authorizeRequests().antMatchers("/favicon.ico")
                .permitAll().anyRequest().access("@RBACService.hasPermission(request, authentication)").and().csrf()
                .disable();
    }

}
